﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface;
using BusinessObjects;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer
{
    public class SqlServerRestaurantMenuNormDao : IRestaurantMenuNormDao
    {
        public List<RestaurantMenuNorm> GetRestaurantMenuNorms() { return null; }

        public List<RestaurantMenuNorm> GetRestaurantMenuNormsByMenuId(string menuId)
        {
            string procedures = "Get_All_RestaurantMenuNorms_ByCondition";

            object[] parms = { "@WhereClause", " WHERE MenuID = '" + menuId + "'" };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<RestaurantMenuNorm> GetRestaurantMenuNormsByBranch(string restaurantId)
        {
            string procedures = "Get_All_RestaurantMenuNorms_ByCondition";

            object[] parms = { "@WhereClause", " WHERE RestaurantID = '" + restaurantId + "'" };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public RestaurantMenuNorm GetRestaurantMenuNorm(string menuNormId)
        {
            string procedures = "Get_All_RestaurantMenuNorm_ByID";

            object[] parms = { "@MenuNormID", menuNormId };
            return Db.Read(procedures, true, Make, parms);
        }

        public void InsertRestaurantMenuNorm(RestaurantMenuNorm restaurantMenuNorm)
        {
            string sql = "Insert_RestaurantMenuNorm";

            restaurantMenuNorm.MenuNormID = Db.Insert(sql, true, Take(restaurantMenuNorm));
        }

        public void UpdateRestaurantMenuNorm(RestaurantMenuNorm restaurantMenuNorm)
        {
            string sql = "Update_RestaurantMenuNorm";

            Db.Update(sql, true, Take(restaurantMenuNorm));
        }

        public void DeleteRestaurantMenuNorm(RestaurantMenuNorm restaurantMenuNorm)
        {
            string sql = "Delete_RestaurantMenuNorm";

            Db.Update(sql, true, Take(restaurantMenuNorm.MenuNormID));
        }

        public List<RestaurantMenuNorm> GetRestaurantMenuNormsPagedByBranch(string restaurantId, int currentPage)
        {
            string procedures = "Get_Paged_RestaurantMenuNorms";

            object[] parms = {  "@WhereClause", " RestaurantID = '" + restaurantId + "'", 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public int GetTotalPageByBranch(string restaurantId)
        {
            string procedures = "Get_PageSize_RestaurantMenuNorm";
            object[] parms = { "@WhereClause", " WHERE RestaurantID = '" + restaurantId + "'" };

            return Db.ReadTotalPage(procedures, true, parms);
        }

        private object[] Take(string menuNormId)
        {
            return new object[]  
            {
                "@MenuNormID", menuNormId,
            };
        }

        private object[] Take(RestaurantMenuNorm restaurantMenuNorm)
        {
            return new object[]  
            {
                "@MenuNormID", restaurantMenuNorm.MenuNormID,
                "@MenuNormCode", restaurantMenuNorm.MenuNormCode,
                "@MenuNormName", restaurantMenuNorm.MenuNormName,
                "@RestaurantID", restaurantMenuNorm.RestaurantID,
                "@Description", restaurantMenuNorm.Description,
                "@MenuID", restaurantMenuNorm.MenuID
            };
        }

        private static Func<IDataReader, RestaurantMenuNorm> Make = reader =>
            new RestaurantMenuNorm
            {
                MenuNormID = reader["MenuNormID"].AsString(),
                MenuNormCode = reader["MenuNormCode"].AsString(),
                MenuNormName = reader["MenuNormName"].AsString(),
                RestaurantID = reader["RestaurantID"].AsString(),
                Description = reader["Description"].AsString(),
                MenuID = reader["MenuID"].AsString()
            };
    }
}
